define(["backbone", "_template", "jquery", "_queryConditions", "_cartData", "app/model/modelNav", "app/view/viewGoods"],
    function (Backbone, template, $, _qc, _CD,Model,viewGoods) {
    return Backbone.View.extend({
        el: '#nav',
        events: {
            "click li": "navItemEvent",
            "click #cartBtn": "cartBtnEvent"
        },
        initialize: function (opts) {
            var scope = this;
            scope.model = new Model();
            scope.model.save({ pointid: _qc.getNetPoint().id }, {
                success: function (data) {
                    if (data.toJSON().success) {
                        var tHtml = template("templateNav", { data: data.toJSON().result });
                        $(scope.el).html(tHtml);
                        scope.render(opts);
                    }
                }
            });
        },
        render: function (opts) {
            var scope = this;
            if (!!opts.viewGoods) {
                this.viewGoods = opts.viewGoods;
            }
            if (!!opts && !!opts.categoryid) {
                var $thisNav = $(scope.el).find("li[data-prodType=" + opts.categoryid + "]");
                if (opts.categoryid == 'cart') {
                    $thisNav = $(scope.el).find("a[data-prodType=" + opts.categoryid + "]");
                }
                scope.switchMenu($thisNav, opts);
            }
            scope.setCartNumb();
        },
        navItemEvent: function (e) {
            var scope = this;
            var $this = $(e.target);
            if (e.target.tagName.toLowerCase() == 'a') {
                $this = $(e.target).parent();
            }
            _qc.setKeyword("");
            if ($this.attr("data-prodtype") == _qc.getCategoryId() && !!scope.viewGoods) {
                scope.viewGoods.render({
                    categoryid: _qc.getCategoryId(),
                    pagesize: _qc.getPageSize(),
                    pageno: _qc.getPageNo(),
                    keyword: _qc.getKeyword(),
                    pointid: _qc.getNetPoint().id
                });
            }
        },
        switchMenu: function ($this) {
            var $cartBtn = $("#cartBtn");
            $(this.el).find("li").removeClass("curr");
            $cartBtn.removeClass("curr");
            $this.addClass("curr");
            //_qc.reset();
        },
        cartBtnEvent: function (e) {
            var $thisNavItem = $(e.target);
        },
        // ��ȡ�����˵�nav��cartԪ��
        getNavCartEl: function () {
            return $("#cartNumb");
        },
        //���ù��ﳵ����Ʒ����
        setCartNumb: function () {
            if (parseInt(_CD.getProductsTotal()) <= 0) {
                this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).show();
            } else {
                this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).show();
            }
        }
    });
});